package fractal;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by IntelliJ IDEA.
 * User: ilya
 * Date: 24.09.11
 * Time: 18:08
 */
public class FractalExplorer extends JFrame implements ActionListener {

    private JButton exportImageButton;
    private JButton savePositionButton;
    private JButton loadPositionButton;
    private JButton zoomInButton;
    private JButton zoomOutButton;
    private JButton moveButton;

    private FractalView fractalView;
    private JLabel coordinatesLabel;

    public static void main(String[] args) {
        new FractalExplorer();
    }

    public FractalExplorer() {
        setTitle("Фрактал");
        createComponents();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//        pack();
        setSize(800, 600);
        setVisible(true);
    }

    private void createComponents() {
        //создать панель для кнопок
        JPanel buttons = new JPanel();

        buttons.add(exportImageButton = new JButton("Сохранить изображение"));
        buttons.add(savePositionButton = new JButton("Сохранить положение"));
        buttons.add(loadPositionButton = new JButton("Загрузить положение"));
        buttons.add(zoomInButton = new JButton("Приблизить"));
        buttons.add(zoomOutButton = new JButton("Удалить"));
        buttons.add(moveButton = new JButton("Переместить"));

        exportImageButton.addActionListener(this);
        savePositionButton.addActionListener(this);
        loadPositionButton.addActionListener(this);
        zoomInButton.addActionListener(this);
        zoomOutButton.addActionListener(this);
        moveButton.addActionListener(this);

        //метка для отображения координат
        coordinatesLabel = new JLabel();

        //отображатель фракталов
        fractalView = new FractalView(
                new MandelbrotFractal(),
//                new AllColorsFractal(0.005),
                new Coordinates(-2.2, -1.4, 0.005)
        );
//        fractalView.setPalette(new GrayPalette());
        fractalView.setPalette(new FancyPalette());

        //расположить элементы на окне
        setLayout(new BorderLayout());

        add(buttons, BorderLayout.NORTH);
        add(fractalView);
        add(coordinatesLabel, BorderLayout.SOUTH);
    }

    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();

        if (source == exportImageButton)
            exportImage();
        else if (source == savePositionButton)
            savePosition();
        else if (source == loadPositionButton)
            loadPosition();
        else if (source == zoomInButton)
            zoomInMode();
        else if (source == zoomOutButton)
            zoomOutMode();
        else if (source == moveButton)
            moveMode();
    }

    private void moveMode() {
        fractalView.setMouseMode(FractalView.MOVE);
    }

    private void zoomOutMode() {
        fractalView.setMouseMode(FractalView.ZOOM_OUT);
    }

    private void zoomInMode() {
        fractalView.setMouseMode(FractalView.ZOOM_IN);
    }

    private void loadPosition() {
        //TODO implement
    }

    private void savePosition() {
        //TODO implement
    }

    private void exportImage() {
        //TODO implement
    }

    private void updateCoordinates() {
        Coordinates coordinates = fractalView.getCoordinates();
        coordinatesLabel.setText(coordinates.getX0() + ", " + coordinates.getY0() + " ==> " + coordinates.getM());
    }
}
